package com.purengage.ipilih;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.ActionText;
import com.markupartist.android.widget.ActionBar.IntentAction;
import com.markupartist.android.widget.ActionBar.IntentActionText;
import com.purengage.ipilih.lib.Answer;
import com.purengage.ipilih.lib.DBClass;
import com.purengage.ipilih.lib.DialogBox;
import com.purengage.ipilih.lib.EntryAdapterDetailRelawan;
import com.purengage.ipilih.lib.EntryItem;
import com.purengage.ipilih.lib.Item;
import com.purengage.ipilih.lib.SectionItem;
import com.purengage.ipilih.lib.Surveys;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class AddResponden extends ListActivity implements OnItemClickListener{
	ArrayList<Item> items = new ArrayList<Item>();
	ListView listview;
	String hh;
	EntryAdapterDetailRelawan adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_add_responden);
		final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		actionBar.setTitle("Responden");
		
		Bundle extras = getIntent().getExtras();
		String id = extras.getString("id");
		
		Intent i = new Intent(this, Survey2.class);
		i.putExtra("id", id);
		
		Intent in = new Intent(this, AddResponden.class);
		in.putExtra("id", id);
		
		actionBar.setHomeAction(new IntentAction(this, i, R.drawable.ic_action_back));

		final ActionText CancelAction = new IntentActionText(this, in, "Cancel");
		actionBar.addAction(CancelAction);

		final ActionText SaveAction = new IntentActionText(this, in, "Save");
		actionBar.addAction(SaveAction);				
		
		DBClass db = new DBClass(this);
		db.deleteTmp();
		Surveys s  = db.getSurveyById(Integer.parseInt(id));
		String ops = s.getOptions();						
		
		listview = getListView();
		
		try {
			
			JSONArray json = new JSONArray(ops);

			for (int j = 0; j < json.length(); j++) {
				String qt = (j>0)?"Pertanyaan":"Responden";
				items.add(new SectionItem(qt));
			
				JSONObject job = json.getJSONObject(j);
				JSONArray js = job.getJSONArray("qset");
				
				for(int z = 0; z < js.length(); z++){
					JSONObject jb = js.getJSONObject(z);
					items.add(new EntryItem(0, jb.getString("q"), "Belum diisi", jb.getString("qid"), jb.getString("aid")));
										
				}								
				
			}	
			
		} catch (JSONException e) {			
			e.printStackTrace();
		}						
		
		adapter = new EntryAdapterDetailRelawan(this, items);
		
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
		EntryItem item = (EntryItem) items.get(pos);		
		
		DBClass db = new DBClass(this);
		Answer a = db.getAnswerById(Integer.parseInt(item.id));				
		
		DialogBox dbox = new DialogBox(AddResponden.this);		
		
		if(a.getInput().equalsIgnoreCase("text")){
			dbox.textFieldPopup(a.getName(),item.subtitle2);
			
		}else if(a.getInput().equalsIgnoreCase("radio")){
			String as = a.getOption().replace("[", "");
			as = as.replace("]", "");
			as = as.replace("\"", "");
			
			String[] qq = as.split(","); 			
			dbox.radioFieldPopup(a.getName(), item.subtitle2, qq);
			
		}else if(a.getInput().equalsIgnoreCase("checkbox")){
			String as = a.getOption().replace("[", "");
			as = as.replace("]", "");
			as = as.replace("\"", "");
			
			String[] qq = as.split(","); 			
			dbox.checkFieldPopup(a.getName(), item.subtitle2, qq);
			
		}
		

		//TextView t = (TextView)findViewById(R.id.text_data);		
		//Log.i("--out--", t.getText().toString());
		
	}

}
